﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace IT_Pro.Class
{
    class CV_BanGiaoLinhKien
    {
        private string _maMay;

        public string MaMay
        {
            get { return _maMay; }
            set { _maMay = value; }
        }
        private DateTime _ngayGiao;

        public DateTime NgayGiao
        {
            get { return _ngayGiao; }
            set { _ngayGiao = value; }
        }
        private string _nguoiGiao;

        public string NguoiGiao
        {
            get { return _nguoiGiao; }
            set { _nguoiGiao = value; }
        }
        private string _nguoiNhan;

        public string NguoiNhan
        {
            get { return _nguoiNhan; }
            set { _nguoiNhan = value; }
        }
        private string _ghiChu;

        public string GhiChu
        {
            get { return _ghiChu; }
            set { _ghiChu = value; }
        }

        private DateTime _hanBaoHanh;

        public DateTime HanBaoHanh
        {
            get { return _hanBaoHanh; }
            set { _hanBaoHanh = value; }
        }

        private string _nhaCungCap;

        public string NhaCungCap
        {
            get { return _nhaCungCap; }
            set { _nhaCungCap = value; }
        }
        
        public static bool BanGiaoLinhKien(CV_BanGiaoLinhKien bglk,DataTable ctbglk)
        {
            //cap nhat vao ban giao link kien
            /*
            string strSQL = "insert into CV_BanGiaoLinhKien(MaMay,NgayGiao,NguoiGiao,NguoiNhan,GhiChu) values ('"+bglk.MaMay+"','"+bglk.NgayGiao+"','"+bglk.NguoiGiao+"','"+bglk.NguoiNhan+"',N'"+bglk.GhiChu+"')";
            DataServices.ExecNonQuery(strSQL);           
            strSQL="select IDBGLK from CV_BanGiaoLinhKien order by IDBGLK Desc";
            DataTable dt=DataServices.ExecQuery(strSQL);
            int id=int.Parse(dt.Rows[0][0].ToString());
            for (int i=0;i<ctbglk.Rows.Count;i++)
            {
                 strSQL="insert into CV_ChiTietBanGiaoLinhKien values ('"+id+"','"+ctbglk.Rows[i]["MaLK"].ToString()+"','"+ctbglk.Rows[i]["TinhTrang"].ToString()+"')";
                 DataServices.ExecNonQuery(strSQL);
                 strSQL = "insert into MT_ChiTietMayTinh(MaMay,LinhKien,HanBaoHanh,NhaCungCap) values ('" + bglk.MaMay + "','" + ctbglk.Rows[i]["MaLK"].ToString() + "','" + ctbglk.Rows[i]["HanBaoHanh"].ToString() + "','" + ctbglk.Rows[i]["NhaCC"].ToString() + "')";
                 DataServices.ExecNonQuery(strSQL);
                 strSQL = "delete from MT_DanhMucLinhKienPhongIT where ID='"+ctbglk.Rows[i]["ID"].ToString()+"'";
                 DataServices.ExecNonQuery(strSQL);
            }
            */
            bool kq;
            string strSQL = "insert into CV_BanGiaoLinhKien(MaMay,NgayGiao,NguoiGiao,NguoiNhan,GhiChu) values ('" + bglk.MaMay + "','" + bglk.NgayGiao + "','" + bglk.NguoiGiao + "','" + bglk.NguoiNhan + "',N'" + bglk.GhiChu + "')";
            DataServices.OpenConnection();
            SqlCommand command = DataServices.connection.CreateCommand();
            SqlTransaction transaction = DataServices.connection.BeginTransaction();
            command.Connection = DataServices.connection;
            command.Transaction = transaction;
            try
            {
                command.CommandText = strSQL;
                command.ExecuteNonQuery();
                command.CommandText = "SELECT @@identity";
                string id = command.ExecuteScalar().ToString();
                for (int i = 0; i < ctbglk.Rows.Count; i++)
                {
                    strSQL = "insert into CV_ChiTietBanGiaoLinhKien values ('" + id + "','" + ctbglk.Rows[i]["MaLK"].ToString() + "','" + ctbglk.Rows[i]["TinhTrang"].ToString() + "')";
                    command.CommandText = strSQL;
                    command.ExecuteNonQuery();
                    strSQL = "insert into MT_ChiTietMayTinh(MaMay,LinhKien,HanBaoHanh,NhaCungCap) values ('" + bglk.MaMay + "','" + ctbglk.Rows[i]["MaLK"].ToString() + "','" + ctbglk.Rows[i]["HanBaoHanh"].ToString() + "','" + ctbglk.Rows[i]["NhaCC"].ToString() + "')";
                    command.CommandText = strSQL;
                    command.ExecuteNonQuery();
                    strSQL = "delete from MT_DanhMucLinhKienPhongIT where ID='" + ctbglk.Rows[i]["ID"].ToString() + "'";
                    command.CommandText = strSQL;
                    command.ExecuteNonQuery();
                }
                transaction.Commit();
                kq = true;
            }
            catch
            {
                transaction.Rollback();
                kq = false;
            }
            DataServices.OpenConnection();
            return kq;
        }

        public static bool BanGiaoLinhKienChoIT(CV_BanGiaoLinhKien bglk, DataTable ctbglk)
        {
                      
            //cap nhat vao ban giao link kien
            /*
            string strSQL = "insert into CV_BanGiaoLinhKien(MaMay,NgayGiao,NguoiGiao,NguoiNhan,GhiChu) values ('" + bglk.MaMay + "','" + bglk.NgayGiao + "','" + bglk.NguoiGiao + "','" + bglk.NguoiNhan + "',N'" + bglk.GhiChu + "')";
            DataServices.ExecNonQuery(strSQL);
            strSQL = "select IDBGLK from CV_BanGiaoLinhKien order by IDBGLK Desc";
            DataTable dt = DataServices.ExecQuery(strSQL);
            int id = int.Parse(dt.Rows[0][0].ToString());
            for (int i = 0; i < ctbglk.Rows.Count; i++)
            {
                strSQL = "insert into CV_ChiTietBanGiaoLinhKien values ('" + id + "','" + ctbglk.Rows[i]["MaLK"].ToString() + "','" + ctbglk.Rows[i]["TinhTrang"].ToString() + "')";
                DataServices.ExecNonQuery(strSQL);               
                strSQL = "delete from MT_ChiTietMayTinh where IDChiTiet='" + ctbglk.Rows[i]["ID"].ToString() + "'";
                DataServices.ExecNonQuery(strSQL);
            }
             
            CapnhatLinhKienPhongIT(ctbglk);
             */
            bool kq;
            string strSQL = "insert into CV_BanGiaoLinhKien(MaMay,NgayGiao,NguoiGiao,NguoiNhan,GhiChu) values ('" + bglk.MaMay + "','" + bglk.NgayGiao + "','" + bglk.NguoiGiao + "','" + bglk.NguoiNhan + "',N'" + bglk.GhiChu + "')";
            DataServices.OpenConnection();
            SqlCommand command = DataServices.connection.CreateCommand();
            SqlTransaction transaction = DataServices.connection.BeginTransaction();
            command.Connection = DataServices.connection;
            command.Transaction = transaction;
            try
            {
                command.CommandText = strSQL;
                command.ExecuteNonQuery();
                command.CommandText = "SELECT @@identity";
                string id = command.ExecuteScalar().ToString();
                for (int i = 0; i < ctbglk.Rows.Count; i++)
                {
                    strSQL = "insert into CV_ChiTietBanGiaoLinhKien values ('" + id + "','" + ctbglk.Rows[i]["MaLK"].ToString() + "','" + ctbglk.Rows[i]["TinhTrang"].ToString() + "')";
                    command.CommandText = strSQL;
                    command.ExecuteNonQuery();
                    strSQL = "delete from MT_ChiTietMayTinh where IDChiTiet='" + ctbglk.Rows[i]["ID"].ToString() + "'";
                    command.CommandText = strSQL;
                    command.ExecuteNonQuery();
                    strSQL = "insert into MT_DanhMucLinhKienPhongIT(MaLinhKien,HanBaoHanh,NhaCC,TinhTrang) values ('" + ctbglk.Rows[i]["MaLK"].ToString() + "','" + ctbglk.Rows[i]["HanBaoHanh"].ToString() + "','" + ctbglk.Rows[i]["NhaCC"].ToString() + "','" + ctbglk.Rows[i]["TinhTrang"].ToString() + "')";
                    command.CommandText = strSQL;
                    command.ExecuteNonQuery();
                }
                transaction.Commit();
                kq = true;
            }
            catch
            {
                transaction.Rollback();
                kq = false;
            }
            DataServices.OpenConnection();
            return kq;
        }


        public static DataTable DanhsachLKPhongIT()
        {
            string strSQL = "Select 0 as STT,* from MT_DanhMucLinhKienPhongIT lkit,MT_DanhMucLinhKien dm, MT_TinhTrang tt  where lkit.MaLinhKien=dm.MaLK and lkit.TinhTrang=tt.IDTT";
            return DataServices.ExecQuery(strSQL);
        }

        public static void CapnhatLinhKienPhongIT(DataTable dt)
        {
            string strSQL="";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                strSQL = "insert into MT_DanhMucLinhKienPhongIT(MaLinhKien,HanBaoHanh,NhaCC,TinhTrang) values ('" + dt.Rows[i]["MaLK"].ToString() + "','" + dt.Rows[i]["HanBaoHanh"].ToString() + "','" + dt.Rows[i]["NhaCC"].ToString() + "','" + dt.Rows[i]["TinhTrang"].ToString() + "')";
                DataServices.ExecNonQuery(strSQL);               
            }

        }
    }

    class CV_CTBanGiaoLinhKien
    {
        private string _maLK;

        public string MaLK
        {
            get { return _maLK; }
            set { _maLK = value; }
        }
        private string _tinhTrang;

        public string TinhTrang
        {
            get { return _tinhTrang; }
            set { _tinhTrang = value; }
        }
    }
}
